package com.vivien.example.springbootdemo.controller;

import com.vivien.example.springbootdemo.pojo.Orders;
import com.vivien.example.springbootdemo.service.OrdersService;
import com.vivien.example.springbootdemo.service.OrderService;
import com.vivien.example.springbootdemo.pojo.Employee;
import com.vivien.example.springbootdemo.pojo.Order;
import com.vivien.example.springbootdemo.pojo.OrderDetail;
import com.vivien.example.springbootdemo.pojo.OrderRequest;
import com.vivien.example.springbootdemo.vo.DataResult;
import com.vivien.example.springbootdemo.vo.Result;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrdersService ordersService;


    @PostMapping("/orders")
    public ResponseEntity<Map<String, Object>> submitInsurance(@RequestBody Orders order) {
        System.out.println(order);
        boolean success = ordersService.save(order);
        if (success) {
            Map<String, Object> responseMap = new HashMap<>();
            responseMap.put("message", "投保成功");
            responseMap.put("orderId", order.getId()); // 假设保存成功后会为订单分配一个 ID
            return ResponseEntity.status(HttpStatus.OK).body(responseMap);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Collections.singletonMap("message", "投保失败"));
        }
    }
//    @GetMapping("/orders/{id}")
//    public ResponseEntity<OrderDetail> getOrderDetail(@PathVariable Long id) {
//        OrderDetail detail = ordersService.getOrderDetailById(id);
//        if (detail != null) {
//            return ResponseEntity.ok(detail);
//        } else {
//            return ResponseEntity.notFound().build();
//        }
//    }
@GetMapping("/orders/{id}")
public ResponseEntity<Orders> getOrderDetail(@PathVariable Long id) {
    Orders order = ordersService.getById(id);
    System.out.println(order);
    if (order != null) {
        return ResponseEntity.ok(order);
    } else {
        return ResponseEntity.notFound().build();
    }
}

}
